﻿var focusObj = null;
$(function(){
	var xOffset = -5; // x distance from mouse
    var yOffset = 5; // y distance from mouse  
	//input action
  
    
    //$("[reg],[ajaxurl]:not([reg]),[tip]").wrap('<span class="valiInputWrap"></span>')
    //$('.valiInputWrap').append('<p class="input_validation-true"></p>')
	$("[reg],[ajaxurl]:not([reg]),[tip]").live("blur",function(){
		focusObj = null;
	    validate(this);
	    showTipsExplain(this,"hide");
	}).live("focus",function(){
		$(this).removeClass("input-validation-failed");
		focusObj = this;
		showTipsExplain(this,"show");
	}).change(function(){
	    validate(this);
	});
	
	$("form").submit(function(){
	    return validateForm(this);
	});
});

function initValidateForm(){
	var xOffset = -5; // x distance from mouse
    var yOffset = 5; // y distance from mouse  
	//input action
  
    
    //$("[reg],[ajaxurl]:not([reg]),[tip]").wrap('<span class="valiInputWrap"></span>')
   // $('.valiInputWrap').append('<p class="input_validation-true"></p>')
	$("[reg],[ajaxurl]:not([reg]),[tip]").live("blur",function(){
		focusObj = null;
	    validate(this);
	    showTipsExplain(this,"hide");
	}).live("focus",function(){
		$(this).removeClass("input-validation-failed");
		focusObj = this;
		 showTipsExplain(this,"show");

	}).change(function(){
	    validate(this);
	});
	
	$("form").submit(function(){
	    return validateForm(this);
	});
}


function validate(target){
    if($(target).attr("ajaxurl") != undefined){
        ajax_validate($(target));
    }else if($(target).attr("check") != undefined){
        diyValidate($(target).attr("check"),$(target));
    }else if($(target).attr("reg") == "CheckboxMenu"){
        checkboxMenuValidate($(target));
    }else if($(target).attr("reg") == "MultipleEnter"){
        multipleEnterValidate($(target));
    }else if($(target).attr("reg") != undefined){
        commonValidate($(target));
    }
}

function checkboxMenuValidate(target){
    var isValid = false;
    var menu = Qfang.MenuManager.getMenu(target.attr("id"));
    if (menu) {
        var result = menu.getValue();
        if (result && result.length >= 1) {
            isValid = true;
        }
    }
    return setStyle(isValid,target);
}

function multipleEnterValidate(target){
    var isValid = false;
    var multipleEnter = qfang.multiple.Manager.getMultipleEnter(target.find("input").attr("id"));
    if (multipleEnter) {
        var result = multipleEnter.getValue();
        if (result && result.length >= 1) {
            isValid = true;
        }
    }
    return setStyle(isValid,target);
}

function diyValidate(cb,target){
    return setStyle(eval(cb+"(target)"),target);
}

function setStyle(isValid,target){
    if(isValid){
        change_error_style(target,"remove");
        change_tip(target,null,"remove");
        return isValid;
    }else{
        change_error_style(target,"add");
        change_tip(target,null,"remove");
        return isValid;
    }
}

function validateForm(obj){
		var isSubmit = true;
		$(obj).find("[reg],[ajaxurl]:not([reg])").each(function(){
			 if($(this).attr("ajaxurl") != undefined){
				if(!ajax_validate($(this))){
					isSubmit = false;
				}
			}else if($(this).attr("check") != undefined){
                if(!diyValidate($(this).attr("check"),$(this))){
                    isSubmit = false;
                }
            }else if($(this).attr("reg") == "CheckboxMenu"){
                if(!checkboxMenuValidate($(this))){
                    isSubmit = false;
                }
            }else if($(this).attr("reg") == "MultipleEnter"){
                if(!multipleEnterValidate($(this))){
                    isSubmit = false;
                }
            }else{ 
				if(!commonValidate($(this))){
					isSubmit = false;
				}
			 }
		});
		if(typeof(isExtendsValidate) != "undefined"){
   			if(isSubmit && isExtendsValidate){
				return extendsValidate();
			}
   		}
		 
		return isSubmit; 
}

function commonValidate(obj){
	var objValue = $.trim(obj.attr("value"));
	regText=obj.attr("reg")
	var maxNumber=0; //最大字符数
	var minNumber=0; //最小字符数
	var allowNull = false;	//输入框允许为空值
	if(obj.attr("maxNumber")){
		maxNumber = parseInt(obj.attr("maxNumber"));
	}
	if(obj.attr("minNumber")){
		minNumber = parseInt(obj.attr("minNumber"));
	}
	if(obj.attr("allowNull")){
		allowNull = true;
	}
	
	var mapValue="0,0";
 	switch(regText){
		case "number" :
			regChar=/^[0-9]*$/;
		break;
		case "Float" :
			regChar=/^[0-9]+((\.[0-9]{1,2})||([0-9]*))$/;
		break;
		case "Email" :
			regChar=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		break;
		case "Chinese":
			regChar=/^[\u4e00-\u9fa5]+$/;	
		break;
		case "Phone":
			regChar=/^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
		break;
		case "Required":
			regChar=/.+/;	
		break;
		case "IdCard":
			regChar=/\d{15}|\d{18}/;
		break;
		case "Password":
			regChar=/\S{6,}/;
		break;
		case "Mobile":
			regChar=/^[1][3458]\d{9}$/;
		break;
		case "ContactPhone" :
			  regChar=/(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[358]\d{9})$)/;
		break;
		case "JobNumber" :
			regChar=/^[0-9]{4,10}$/;
		break;	
		case "isPhone":
			  regChar=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
		break;
		case "MapCheck":  //提示判断地图
			  mapValue=objValue;
			 regChar='';
		break;
		case "OnlyLength":
			 regChar='';
		break;
		case "NoSign":
			 regChar='^[^<>]*$';
		break;
		case "ChEnNumber":
			 regChar=/^(\w|[\u4E00-\u9FA5])*$/;
			 break;
		case "banckCard":
			regChar=''; 
		default:
		;
	}
 
	// alert(regText)
	var reg = new RegExp(regChar);
	var isMobile=/^(?:13\d|15\d)\d{5}(\d{3}|\*{3})$/;   
	if((objValue==null || objValue=="") && allowNull == false){
		change_error_style(obj,"add");
		change_tip(obj,null,"remove");
		return false;
	}else{
		//判断手机号码和座机号码
		if(regText == "isPhone" && objValue != ""){
			if(!reg.test(objValue) && !isMobile.test(objValue)){
				change_error_style(obj,"add");
				change_tip(obj,null,"remove");
				return false;	
			}
		}else if(regText=="IdCard"){
			if(!IdCardValidate(objValue)){
				change_error_style(obj,"add");
				change_tip(obj,null,"remove");
				return false;
			}
		}else if(regText=="banckCard"){
			regChar=/^[0-9]*$/;
			var reg2 = new RegExp(regChar);
			if(!reg2.test(objValue) || objValue.length < 16 || objValue.length >= 25){
				change_error_style(obj,"add");
				change_tip(obj,null,"remove");
				return false;	
			}
			
		}else{
			if(regChar && objValue){
				if(!reg.test(objValue)){
					change_error_style(obj,"add");
					change_tip(obj,null,"remove");
					return false;
				}
			}else{
				if(regText =="MapCheck" &&  mapValue == "0,0"){
					change_error_style(obj,"add");
					change_tip(obj,null,"remove");
					return false;
				}
			}
			if(maxNumber > 0 && objValue.length > maxNumber){
				change_error_style(obj,"add");
				change_tip(obj,null,"remove");
				return false;
			}
			if(minNumber > 0 && objValue.length < minNumber){
				change_error_style(obj,"add");
				change_tip(obj,null,"remove");
				return false;
			}
		}
		if(obj.attr("ajaxurl") == undefined){
			if(allowNull == true && objValue.length < 1){
				hideTips(obj);
				return true;
			}else{
				change_error_style(obj,"remove");
				change_tip(obj,null,"remove");
				return true;
			}
		}else{
			return ajax_validate(obj);
		}
	}
}
/**  
 * 身份证15位编码规则：dddddd yymmdd xx p   
 * dddddd：地区码   
 * yymmdd: 出生年月日   
 * xx: 顺序类编码，无法确定   
 * p: 性别，奇数为男，偶数为女  
 * <p />  
 * 身份证18位编码规则：dddddd yyyymmdd xxx y   
 * dddddd：地区码   
 * yyyymmdd: 出生年月日   
 * xxx:顺序类编码，无法确定，奇数为男，偶数为女   
 * y: 校验码，该位数值可通过前17位计算获得  
 * <p />  
 * 18位号码加权因子为(从右到左) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]  
 * 验证位 Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]   
 * 校验位计算公式：Y_P = mod( ∑(Ai×Wi),11 )   
 * i为身份证号码从右往左数的 2...18 位; Y_P为脚丫校验码所在校验码数组位置  
 *   
 */  
  
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];// 加权因子   
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];// 身份证验证位值.10代表X   
function IdCardValidate(idCard) {   
    idCard = trim(idCard.replace(/ /g, ""));   
    if (idCard.length == 15) {   
        return isValidityBrithBy15IdCard(idCard);   
    } else if (idCard.length == 18) {   
        var a_idCard = idCard.split("");// 得到身份证数组   
        if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){   
            return true;   
        }else {   
            return false;   
        }   
    } else {   
        return false;   
    }   
}   
/**  
 * 判断身份证号码为18位时最后的验证位是否正确  
 * @param a_idCard 身份证号码数组  
 * @return  
 */  
function isTrueValidateCodeBy18IdCard(a_idCard) {   
    var sum = 0; // 声明加权求和变量   
    if (a_idCard[17].toLowerCase() == 'x') {   
        a_idCard[17] = 10;// 将最后位为x的验证码替换为10方便后续操作   
    }   
    for ( var i = 0; i < 17; i++) {   
        sum += Wi[i] * a_idCard[i];// 加权求和   
    }   
    valCodePosition = sum % 11;// 得到验证码所位置   
    if (a_idCard[17] == ValideCode[valCodePosition]) {   
        return true;   
    } else {   
        return false;   
    }   
}   
/**  
 * 通过身份证判断是男是女  
 * @param idCard 15/18位身份证号码   
 * @return 'female'-女、'male'-男  
 */  
function maleOrFemalByIdCard(idCard){   
    idCard = trim(idCard.replace(/ /g, ""));// 对身份证号码做处理。包括字符间有空格。   
    if(idCard.length==15){   
        if(idCard.substring(14,15)%2==0){   
            return 'female';   
        }else{   
            return 'male';   
        }   
    }else if(idCard.length ==18){   
        if(idCard.substring(14,17)%2==0){   
            return 'female';   
        }else{   
            return 'male';   
        }   
    }else{   
        return null;   
    }   

}   
 /**  
  * 验证18位数身份证号码中的生日是否是有效生日  
  * @param idCard 18位书身份证字符串  
  * @return  
  */  
function isValidityBrithBy18IdCard(idCard18){   
    var year =  idCard18.substring(6,10);   
    var month = idCard18.substring(10,12);   
    var day = idCard18.substring(12,14);   
    var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));   
    // 这里用getFullYear()获取年份，避免千年虫问题   
    if(temp_date.getFullYear()!=parseFloat(year)   
          ||temp_date.getMonth()!=parseFloat(month)-1   
          ||temp_date.getDate()!=parseFloat(day)){   
            return false;   
    }else{   
        return true;   
    }   
}   
  /**  
   * 验证15位数身份证号码中的生日是否是有效生日  
   * @param idCard15 15位书身份证字符串  
   * @return  
   */  
  function isValidityBrithBy15IdCard(idCard15){   
      var year =  idCard15.substring(6,8);   
      var month = idCard15.substring(8,10);   
      var day = idCard15.substring(10,12);   
      var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));   
      // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法   
      if(temp_date.getYear()!=parseFloat(year)   
              ||temp_date.getMonth()!=parseFloat(month)-1   
              ||temp_date.getDate()!=parseFloat(day)){   
                return false;   
        }else{   
            return true;   
        }   
  }   
//去掉字符串头尾空格   
function trim(str) {   
    return str.replace(/(^\s*)|(\s*$)/g, "");   
} 
/*function ajax_validate(obj){
	
	var url_str = obj.attr("ajaxurl");
	if(url_str.indexOf("?") != -1){
		url_str = url_str+"&"+obj.attr("name")+"="+obj.attr("value");
	}else{
		url_str = url_str+"?"+obj.attr("name")+"="+obj.attr("value");
	}
	var feed_back = $.ajax({url: url_str,cache: false,async: false}).responseText;
	feed_back = feed_back.replace(/(^\s*)|(\s*$)/g, "");
	if(feed_back == 'success'){
		change_error_style(obj,"remove");
		change_tip(obj,null,"remove");
		return true;
	}else{
		change_error_style(obj,"add");
		change_tip(obj,feed_back,"add");
		return false;
	}
}*/
function ajax_validate(obj){
/*	if(dataName == undefined ){
		var dataVal_str = obj.val();
	}else{
		var dataVal_str= dataName;
	}*/
	var url_str = obj.attr("ajaxurl");
	var dataName_str =obj.attr("name");
	var dataVal_str = obj.val();
	// alert(dataName_str + "," + dataVal_str )
	var feed_back = $.ajax({url: url_str,cache: false,async: false,data:{number:dataVal_str}}).responseText;
	feed_back = feed_back.replace(/(^\s*)|(\s*$)/g, "");
	if(feed_back == 'success'){
		change_error_style(obj,"remove");
		change_tip(obj,null,"remove");
		return true;
	}else{
		change_error_style(obj,"add");
		change_tip(obj,feed_back,"add");
		return false;
	}
}



function change_tip(obj,msg,action_type){
	
	if(obj.attr("tip") == undefined){//初始化判断TIP是否为空
		obj.attr("is_tip_null","yes");
	}
	if(action_type == "add"){
		if(obj.attr("is_tip_null") == "yes"){
			obj.attr("tip",msg);
		}else{
			if(msg != null){
				if(obj.attr("tip_bak") == undefined){
					obj.attr("tip_bak",obj.attr("tip"));
				}
				obj.attr("tip",msg);
			}
		}
	}else{
		if(obj.attr("is_tip_null") == "yes"){
			obj.removeAttr("tip");
			obj.removeAttr("tip_bak");
		}else{
			obj.attr("tip",obj.attr("tip_bak"));
			obj.removeAttr("tip_bak");
		}
	}
}

function ChangeTips(obj,type,msg){
	var parent=obj.parent()
	var oWidth=$(obj).outerWidth();
	var oHeight=$(obj).outerHeight();
	var oX=$(obj).offset().left;
	var oY=$(obj).offset().top;
	var posY=oY+oHeight+4;
	var posX=oX;
	var thisTureId=obj.attr("id")+"_trueTips";
	var tId=obj.attr("id");
	var tureTipsId="vTip_true_"+tId;//提示正确的对象ID
	var falseTips="vTip_false_"+tId;//提示错误的对象ID
	var tipText ;
	if(msg){
		tipText = msg;
	}else{
		tipText = obj.attr("tip") ;
	}
	if(type=='false'){ 
		if($(obj).attr('tip') != undefined){
			var tipsPosition=$(obj).attr('tip-position');
			var tipsPositionClass;
			if(tipsPosition=="top"){
				tipsPositionClass="validatTips-pos-top";
			}
			if($("#"+falseTips).length <1 ){
				$("body").append('<div id="'+falseTips+'" class="validatTips-failure '+tipsPositionClass+'"><span class="t-arrow"></span><div class="t-cont" style=" ">' + tipText + '</div></div>' );
				$("#"+falseTips).css({"left":oWidth+oX+"px","top":oY+"px"});
				if(tipsPositionClass){
					$("#"+falseTips).css({"left":oX+"px","top":oY+oHeight+"px"});
				}
			}else{
				$("#"+falseTips).show()
			}
		}
		$("#"+tureTipsId).hide();
		
	}else{
		if($(obj).attr('tip') != undefined){
			$("#"+falseTips).remove()
			if($("#"+tureTipsId).length < 1 ){
				$("body").append( '<div id="'+tureTipsId+'" class="validatTips-success"></div>' );
				$("#"+tureTipsId).css({"left":oWidth+oX-8+"px","top":oY-8+"px","display":"block"});
			}else{
				$("#"+tureTipsId).show()
			}
		}	
	}

}
 
function showTipsExplain(obj,type){
	var oWidth=$(obj).outerWidth();
	var oHeight=$(obj).outerHeight();
	var oX=$(obj).offset().left;
	var oY=$(obj).offset().top;
	var posY=oY+oHeight+4;
	var posX=oX;
	var tid=$(obj).attr("id");
	var expTid="validatTips_exp_"+tid;
	var expContent=$(obj).attr("explain")
	var tureTipsId="vTip_true_"+tid;//提示正确的对象ID
	var falseTips="vTip_false_"+tid;//提示错误的对象ID
	if($(obj).attr("explain") != undefined){
		if(type=="show"){
			if($("#"+expTid).length < 1 ){
				$("body").append('<div id="'+expTid+'" class="validatTips-failure validatTips-focusInfo "><span class="t-arrow"></span><div class="t-cont" style=" ">' +expContent + '</div></div>' );
				$("#"+expTid).css({"left":oWidth+oX+"px","top":oY+"px"});
				
			} 
			$("#"+falseTips).remove()
			$("#"+tureTipsId).remove()
		}else{
			$("#"+expTid).remove()
	
		}
	}
}

function resetValidate(formObj){
	$(formObj).find('.input-validation-failed').each(function(){
		$(this).removeClass('input-validation-failed');
 	}) 
 	$(formObj).find('.validatTips-success').each(function(){
 		$(this).remove();
 	})
 	$(formObj).find('.validatTips-failure').each(function(){
 		$(this).remove();
 	})
 	
	
}
function change_error_style(obj,action_type,msg){
	if(action_type == "add"){
		obj.addClass("input-validation-failed");
		ChangeTips(obj,'false',msg);
	}else{
		obj.removeClass("input-validation-failed");
		ChangeTips(obj,'true',msg);
	}
}

function hideTips(obj){
	var tId=obj.attr("id");
	var tureTipsId="vTip_true_"+tId;//提示正确的对象ID
	var falseTips="vTip_false_"+tId;//提示错误的对象ID
	$('#'+tureTipsId).hide();
	$('#'+falseTips).hide();
}

$.fn.validate_callback = function(msg,action_type,options){
	this.each(function(){
		if(action_type == "failed"){
			change_error_style($(this),"add");
			change_tip($(this),msg,"add");
		}else{
			change_error_style($(this),"remove");
			change_tip($(this),null,"remove");
		}
	});
};
